<?php

/**
 * alltosun.com 找回密码 getback.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址:   http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 张元昊 (zhangyh@alltosun.com) $
 * $Date: 2015-1-19 上午3:10:46 $
 * $Id$
*/

class Action
{
    
    private $user_id;
    
    public function __construct()
    {
        $this->user_id = user_helper::get_user_id();
    
    }
    
    public function get_back()
    {
        if($this->user_id) {
            Response::redirect('user/setting');
            return false;
        }

        display_template('get_back.html');

        // if(is_mobile()) {
        //     Response::display('m/get_back.html');
        // } else {
        //     Response::display('get_back.html');
        // }
    }
    
    public function check_identity()
    {
        if($this->user_id) {
            if(Request::isAjax()){
                return array('info' => 'error', 'msg' => '已经登录，无法申请找回密码。');
            }
            Response::redirect('user/setting');
            return false;
        }
        
        $username = Request::Post('username', '');
        if(empty($username)) {
            if(Request::isAjax()){
                return array('info' => 'error', 'msg' => '您填写的手机号或邮箱不能为空！');
            }
            Response::redirect('user/getback/get_back');
        }
        
        $get_way = user_helper::get_user_login_way($username);
        switch ($get_way) {
            case '1':
                $filter['email'] = $username;
                $username_type = 'email';
                break;
            case '2':
                $filter['mobile'] = $username;
                $username_type = 'mobile';
                break;
            
            default:
                return array('info' => 'error','msg' => '请输入正确的手机号或者邮箱！');
                break;
        }
        $user_info = _uri('user', $filter);
        if(!$user_info) {
            if(Request::isAjax()){
                return array('info' => 'error', 'msg' => '您填写的手机号或者邮箱不存在！');
            }
            return array('您填写的手机号或者邮箱不存在！', 'error', AnUrl('user/getback/get_back'));
        } else {
            if(empty($user_info['password']) && !empty($user_info['user_from'])) {
                if(Request::isAjax()){
                    return array('info' => 'error', 'msg' => '您是第三方登录用户，不支持找回密码！');
                }
                return array('您是第三方登录用户，不支持找回密码！', 'error', AnUrl('user/getback/get_back'));
            } else {
                if(Request::isAjax()){
                    return array('info' => 'succeed', 'msg' => 'ok');
                }
            }
        }
        if($username_type == 'email') {
            $result = user_helper::set_mail_send($user_info['email'], $user_info['id'], 'getback');
            if(!$result) {
                return array('网络故障，邮件发送失败，请重试。', 'error', AnUrl('user/getback/get_back'));
            }
        }
        Response::assign('user_info', $user_info);
        Response::assign('username_type', $username_type);
        display_template('check_identity.html');
        // if(is_mobile()) {
        //     Response::display();
        // } else {
        //     Response::display('check_identity.html');
        // }
    }
    
    public function get_mobile_code()
    {
        $mobile = Request::Post('mobile', '');
        $send_type = Request::Post('send_type', '');
        if($mobile) {
            $user_info = _uri('user', array('mobile' => $mobile));
            if(!$user_info) {
                return array('info' => 'error', 'msg' => '短信发送失败，请重试！');
            }
            $mobile = $user_info['mobile'];
            $return_info = sms_helper::sendRegSms($mobile, $send_type);
            if(!$return_info) {
                return array('info' => 'error', 'msg' => '短信发送失败，请重试！');
            } else {
                return array('info' => 'succeed');
            }
        }
    }
    
    public function set_newpass()
    {
        if($this->user_id) {
            Response::redirect('user/setting');
            return false;
        }
        $mobile     = Request::Post('mobile', '');
        if(!$mobile) {
            $code  = Request::Get('code', '');
            $email = Request::Get('email', '');
            $type  = Request::Get('type', '');
            if($code == '' || $email == '' || $type == '' || !in_array($type, mail_config::$send_mail_type)) {
                return array('邮箱验证链接已经失效', 'error', AnUrl('user/setting'));
            }
            $code_info = _uri('email_code', array('code' => $code, 'email' => $email));
            if($code_info) {
                $user_id = $code_info['user_id'];
                $user_info = _uri('user', $user_id);
                if(!$user_info) {
                    return array('邮箱验证链接已经失效', 'error', AnUrl('user/setting'));
                }
            } else {
                return array('邮箱验证链接已经失效', 'error', AnUrl('user/setting'));
            }
            $_SESSION['GBUserTypeARR']['type'] = 'email';
            $_SESSION['GBUserTypeARR']['content'] = $email;
            $_SESSION['GBUserTypeARR']['code'] = $code;
        } else {
            $user_info = _uri('user', array('mobile' => $mobile));
            if(!$user_info) {
                return array('您输入验证码不正确，请重新填写。','error', AnUrl('user/getback/check_identity'));
            }
        }
        $mobile_code = Request::Post('mobile_code', '');
        $email_code  = Request::Get('code', '');
        if(!empty($mobile_code)) {
            $return_code = user_helper::getMobileCode($user_info['mobile'], 'getback');
            if($return_code['code'] != $mobile_code) {
                if(Request::isAjax()) {
                    return array('info' => 'error', 'msg' => '您输入验证码不正确。');
                }
                return array('您输入验证码不正确，请重新填写。','error', AnUrl('user/getback/check_identity'));
            } else {
                if(Request::isAjax()) {
                    return array('info' => 'succeed', 'msg' => 'ok');
                }
                _model('mobile_code')->update(array('mobile' => $user_info['mobile'], 'code' => $mobile_code, 'res_name' => 'getback'), array('status' => 1));
            }
            $_SESSION['GBUserTypeARR']['type'] = 'mobile';
            $_SESSION['GBUserTypeARR']['content'] = $user_info['mobile'];
            $_SESSION['GBUserTypeARR']['code'] = $mobile_code;
        }
        // Response::assign('user_info', $user_info);
        display_template('set_newpass.html');
        // if(is_mobile()) {
        //     Response::display('m/set_newpass.html');
        // } else {
        //     Response::display('set_newpass.html');
        // }
    }
    
    public function get_success()
    {
        if($this->user_id) {
            Response::redirect('user/setting');
            return false;
        }
        // $user_id    = Request::Post('user_id', 0);
        // $check_type = Request::Post('check_type', '');
        $password   = AnFilter::filter_string(trim(Request::getParam('password', '')));
        $repassword = AnFilter::filter_string(trim(Request::getParam('repassword', '')));
        $hash = user_helper::random_hash(6);
        // if(!$user_id || !$check_type) {
        //     if(Request::isAjax()) {
        //         return array('info' => 'error', 'msg' => '参数错误！');
        //     }
        //     Response::redirect('');
        // }
        $check_arr = $_SESSION['GBUserTypeARR'];
        if(!$check_arr) {
            return array('参数错误', 'error', AnUrl('user/getback/get_back'));
        }
        if($check_arr['type'] == 'mobile') {
            $user_info = _uri('user', array('mobile' => $check_arr['content']));
            $check_info = _model('mobile_code')->read(array('mobile' => $user_info['mobile'], 'code' => $check_arr['code']));
        } elseif($check_arr['type'] == 'email') {
            $user_info = _uri('user', array('email' => $check_arr['content']));
            $check_info = _model('email_code')->read(array('email' => $user_info['email'], 'code' => $check_arr['code']));
        } else {
            unset($_SESSION['GBUserTypeARR']);
            if(Request::isAjax()) {
                return array('info' => 'error', 'msg' => '参数错误!');
            }
            return array('参数错误!', 'error', AnUrl('user/getback/get_back'));
        }
        if(!$check_info) {
            unset($_SESSION['GBUserTypeARR']);
            if(Request::isAjax()) {
                return array('info' => 'error', 'msg' => '参数错误!');
            }
            return array('参数错误!', 'error', AnUrl('user/getback/get_back'));
        }

        if(user_helper::is_pwd_null($password)) {
            if(Request::isAjax()) {
                return array('info' => 'error', 'msg' => '您提交的密码内不能包含空字符！');
            }
            return array('您提交的密码内不能包含空字符！', 'error', AnUrl('user/getback/set_newpass'));
        } else {
            if(!user_helper::is_strlen_scope($password, 6, 20)) {
                if(Request::isAjax()) {
                    return array('info' => 'error', 'msg' => '您提交的密码长度不符合要求！');
                }
                return array('您提交的密码长度不符合要求！', 'error', AnUrl('user/getback/set_newpass'));
            }
        }

        if (empty($password) || empty($repassword)) {
            if(Request::isAjax()) {
                return array('info' => 'error', 'msg' => '两次输入的密码都不能为空');
            }
            return array('两次输入的密码都不能为空','error', AnUrl('user/getback/set_newpass'));
        }
        
        if ($password != $repassword) {
            if(Request::isAjax()) {
                return array('info' => 'error', 'msg' => '两次密码不匹配');
            }
            return array('两次密码不匹配','error', AnUrl('user/getback/set_newpass'));
        }
        // $user_info = _uri('user', $user_id);
        if($user_info['password'] == user_helper::md5_password($password, $user_info['hash'])) {
            if(Request::isAjax()) {
                return array('info' => 'error', 'msg' => '新密码不能和原密码一致。');
            }
            return array('新密码不能和原密码一致。','error', AnUrl('user/getback/set_newpass'));
        }
        if(Request::isAjax()) {
            return array('info' => 'ok');
        }
        $info = array(
            'password' => user_helper::md5_password($password, $hash),
            'hash'     => $hash
        );
        $return_info = user_helper::uc_edit($user_info['uc_id'], $user_info['user_name'], $user_info['email'], $password);
        if($return_info['info'] == 'ok') {
            _model('user')->update($user_info['id'], $info);
            display_template('get_success.html');
            // if(is_mobile()) {
            //     Response::display('m/get_success.html');
            // } else {
            //     Response::display('get_success.html');
            // }
        } else {
            return array('修改失败！','error', AnUrl('user/getback/set_newpass'));
        }
        
    }
}
?>